<template>
  <el-breadcrumb
    separator="/"
    class="mb-1"
    :class="{ 'mx-15': useLocale.isMob }"
    style="vertical-align: bottom"
  >
    <el-breadcrumb-item>
      <div class="flex align-center">
        <el-icon :size="18"> <Goods /> </el-icon
        ><span class="pl">电商管理</span>
      </div>
    </el-breadcrumb-item>
    <el-breadcrumb-item>收单管理</el-breadcrumb-item>
  </el-breadcrumb>

  <div v-if="useLocale.isMob">
    <div class="mx-15 my-15 flex justify-end justify-between">
      <el-button type="primary" size="small" plain round @click="handleAsync"
        >同步</el-button
      >
      <el-button
        :icon="Search"
        size="small"
        circle
        @click="
          () => {
            mobFilter = true;
          }
        "
      />
    </div>

    <div class="mob-list mt-2">
      <el-scrollbar ref="scrollbarRef" class="scroll">
        <div
          class="mob-list-item text-content-color font"
          v-for="(item, index) in tableData"
        >
          <div class="border-bottom pb-15">
            <div class="flex justify-between mb-1">
              <span class="font text-tips-color">{{ item.post_time }}</span>
              <el-link :href="item.fb_url" type="primary" target="_blank"
                >查看原文</el-link
              >
            </div>
            <div class="line-height15">{{ item.post_content }}</div>
          </div>
          <div class="flex align-center justify-between border-bottom py-1">
            <span>抓单</span>
            <el-switch v-model="item.catch" />
          </div>
          <div class="flex align-center justify-between border-bottom py-1">
            <span>商品设定</span>
            <el-radio-group v-model="item.set" size="small">
              <el-radio-button v-for="ele in rel.set_arr" :value="ele.id">{{
                ele.name
              }}</el-radio-button>
            </el-radio-group>
          </div>
          <div class="flex align-center justify-between border-bottom py-1">
            <span>直播通知</span>
            <el-switch v-model="item.live_notice" />
          </div>
          <div class="flex align-center justify-between border-bottom py-1">
            <span>结单通知</span>
            <el-switch v-model="item.order_notice" />
          </div>
          <div wrap class="flex flex-wrap justify-end my-1">
            <el-button
              v-if="item.status_id != 2"
              class="mb-1"
              size="small"
              plain
              round
              @click="openStatus(index, item)"
              >标记状态</el-button
            >

            <el-button
              class="mb-1"
              size="small"
              plain
              round
              @click="openGroup(index, item)"
              >设置分组</el-button
            >
            <el-button
              class="mb-1"
              size="small"
              plain
              round
              type="primary"
              @click="dialogue(index, item)"
              >对话</el-button
            >
          </div>
        </div>
      </el-scrollbar>
    </div>

    <div v-if="total > params.page_size" class="my-1 flex justify-center">
      <el-pagination
        v-model:current-page="params.current_page"
        v-model:page-size="params.page_size"
        :pager-count="5"
        :small="false"
        :disabled="false"
        :background="true"
        layout="prev, pager, next"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
  <el-card v-else class="box-card" shadow="hover">
    <template #header>
      <div class="flex justify-between flex-wrap">
        <el-space wrap :size="10">
          <el-input
            v-model="params.query_post_content"
            style="width: 150px"
            placeholder="貼文摘要"
          />
          <el-button :icon="Search" plain @click="search">查詢</el-button>
        </el-space>
        <el-button type="primary" plain @click="handleAsync">同步</el-button>
      </div>
    </template>
    <el-table :data="tableData" style="width: 100%" @row-click="rowClick">
      <el-table-column label="贴文列表">
        <template #default="scope">
          <el-space :size="10"
            ><span class="text-tips-color">{{ scope.row.post_time }}</span>
            <span class="copy" v-if="scope.row.copy">副本</span>
            <el-link
              v-else
              :href="scope.row.fb_url"
              type="primary"
              target="_blank"
              >查看原文</el-link
            ></el-space
          >
          <div>{{ scope.row.post_content }}</div>
        </template>
      </el-table-column>
      <el-table-column label="抓单" width="100">
        <template #default="scope">
          <el-switch
            v-model="scope.row.catch"
            @change="(v) => handleChange(v, 'catch', scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="商品设定" width="150">
        <template #default="scope">
          <el-radio-group v-model="scope.row.set" size="small"   @change="(v) => handleSet(v, scope.row)">
            <el-radio-button
              v-for="item in rel.set_arr"
              :value="item.id"
             
              >{{ item.name }}</el-radio-button
            >
          </el-radio-group>
        </template>
      </el-table-column>
      <el-table-column label="直播通知" width="100">
        <template #default="scope">
          <el-switch
            v-model="scope.row.live_notice"
            @change="(v) => handleChange(v, 'live_notice', scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column label="结单通知" width="100">
        <template #default="scope">
          <el-switch
            v-model="scope.row.order_notice"
            @change="(v) => handleChange(v, 'order_notice', scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template #default="scope">
          <div class="ec2-btncon">
            <el-button
              link
              size="small"
              type="primary"
              @click="handleScene(scope.$index, scope.row)"
              >现场管理</el-button
            >
            <el-button
              link
              size="small"
              type="danger"
              @click="handleStat(scope.$index, scope.row)"
              >统计</el-button
            >
            <el-button
              link
              size="small"
              type="info"
              @click="openGroup(scope.$index, scope.row)"
              >抽奖</el-button
            >
            <el-button
              link
              size="small"
              type="warning"
              @click="openGroup(scope.$index, scope.row)"
              >设置</el-button
            >
          </div>
        </template>
      </el-table-column>
    </el-table>
    <div v-if="total > params.page_size" class="mt-2 flex justify-center">
      <el-pagination
        v-model:current-page="params.current_page"
        v-model:page-size="params.page_size"
        :page-sizes="[10, 20, 30, 40]"
        :small="false"
        :disabled="false"
        :background="true"
        layout="total, sizes, prev, pager, next"
        :total="Number(total)"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </el-card>
  <el-drawer v-model="mobFilter" size="80%">
    <template #header>
      <h4>筛选</h4>
    </template>
    <template #default>
      <el-space direction="vertical" fill style="width: 100%">
        <div class="mb-1">
          <div class="font-sm text-tips-color mb">讯息内容</div>
          <el-input v-model="params.keyword" />
        </div>
        <div class="mb-1">
          <div class="font-sm text-tips-color mb">用戶名稱</div>
          <el-input v-model="params.fb_name" />
        </div>

        <div class="mb-1">
          <div class="font-sm text-tips-color mb">状态</div>
          <el-select v-model="params.status_id" style="width: 100%">
            <el-option
              v-for="item in filter.statusArr"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </div>
      </el-space>
    </template>
    <template #footer>
      <div style="flex: auto">
        <el-button @click="mobClearFilter">清空</el-button>
        <el-button type="primary" @click="search">查询</el-button>
      </div>
    </template>
  </el-drawer>
</template>
<script lang="ts" setup>
import { ref, reactive, onMounted, watch, computed, nextTick } from "vue";
import { Search, Goods } from "@element-plus/icons-vue";
import {
  $getPosting,
  $isCatch,
  $isLiveNotice,
  $isOrderNotice,
  $updateGoodsSet,
} from "@api/live";
import { useI18n } from "vue-i18n";
import { useLocaleStore } from "@/store/locales";
import { useRoute, useRouter } from "vue-router";
const router = useRouter();
const route = useRoute();
const { t } = useI18n();
const langkey = computed(() => useLocale.locale);
const useLocale = useLocaleStore();
const scrollbarRef = ref(); //滚动条名称
const mobFilter = ref(false);
let params = ref({
  query_post_content: "",
  current_page: 1,
  page_size: 10,
});
const rel=reactive({
set_arr:[{id:"1",name:"全库"},{id:"2",name:"自选"}]
})
const total = ref(0);
const tableData = ref([]);
let rel_switch = reactive({
  item: null,
  oSet:''//原始设置值
});
onMounted(() => {
  getData();
});
const getData = async () => {
  let res = await $getPosting(params.value);
  if (res.code == 0) {
    tableData.value = res.data.table;
    total.value = res.data.total;
    if (useLocale.isMob) {
      nextTick(() => {
        scrollbarRef.value.setScrollTop(0);
      });
    }
  }
};
const handleSizeChange = (val: number) => {
  params.value.page_size = val;
  getData();
  console.log(`${val} items per page`);
};
const handleCurrentChange = (val: number) => {
  params.value.current_page = val;
  getData();
  console.log(`current page: ${val}`);
};
const search = () => {
  console.log(params.value);
  getData();
};
const handleAsync = () => {
  params.value.page_size = 10;
  params.value.current_page = 1;
  params.value.query_post_content = "";
  getData();
};
const handleSet = async (v, item) => {
console.log(v);
// let res = await $updateGoodsSet({ live_id: item.live_id, set: v });
//  if (res.code == 0) {
//     ElMessage.success('操作成功');
//     item['set']=v
//   } else {
//     item['set']=rel_switch.oSet
//     ElMessage.error(res.msg);
   
//   }
};
const rowClick=(row, column,event)=>{
     rel_switch.oSet=row.set
};
const handleChange = async (v, type, item) => {
  console.log(v);
  console.log(type);
  console.log(item);
  let res = null;
  switch (type) {
    case "catch":
      res = await $isCatch({ live_id: item.live_id, catch: v });
      break;
    case "live_notice":
      res = await $isLiveNotice({ live_id: item.live_id, live_notice: v });
      break;
    case "order_notice":
      res = await $isOrderNotice({ live_id: item.live_id, order_notice: v });
      break;
  }
  if (res.code == 0) {
    ElMessage.success('操作成功');
    item[type]=v
  } else {
    item[type]=!v
    ElMessage.error(res.msg);
   
  }
};
const handleStat = (index, row) => {
  console.log(row);
  router.push({
    path: "/live/stat",
    query: {
      live_id: row.live_id,
    },
  });
};
const handleScene = (index, row) => {
  console.log(row);
  const url = router.resolve({
   path: "/scene",
   query: {
     live_id: row.live_id,
     live_type:row.live_type_id
   }
 }).href
 window.open(url, '_blank')

};
const dialogue = (index, row) => {
  console.log(row);
  router.push({
    path: "/setting/messageKf/dialog",
    query: {
      fb_user_id: row.fb_user_id,
      fb_account_id: row.fb_account_id,
      fb_name: row.fb_name,
    },
  });
};
watch(langkey, (newVal, oldVal) => {
  //getFilter();
});
</script>
<style>
.ec2-btncon {
  .el-button {
    margin-bottom: 2px;
    justify-content: flex-start;
  }
  .el-button + .el-button {
    margin-left: 0;
  }
}
</style>
<style lang="scss" scoped>
.mob-list {
  .mob-list-item {
    background: #fff;
    border-radius: 8px;
    padding: 15px;
    margin-bottom: 10px;
  }
  .desc-item {
    padding: 15px 0;
    border-bottom: 1px solid #ebeef5;
  }
}
.scroll {
  height: calc(100vh - 220px);
}
.ec2-btncon {
  display: flex;
  flex-direction: column;
}
.primaryColor {
  color: var(--el-color-primary);
}
.copy {
  color: var(--el-color-danger);
}
</style>
